package org.rsbot.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import org.rsbot.util.GlobalConfiguration;
import org.rsbot.util.GlobalConfiguration.Paths;

public class BotToolBar extends JToolBar
{
  private static final long serialVersionUID = -1861866523519184211L;
  public static final int RUN_SCRIPT = 0;
  public static final int PAUSE_SCRIPT = 1;
  public static final int RESUME_SCRIPT = 2;
  public static final ImageIcon ICON_HOME;
  public static final ImageIcon ICON_BOT;
  public static final Image IMAGE_CLOSE;
  public static final Image IMAGE_CLOSE_OVER;
  private JButton userInputButton;
  private JButton runScriptButton;
  private ActionListener listener;
  private int idx;
  private int inputState = 3;
  private boolean inputOverride = true;

  public BotToolBar(ActionListener paramActionListener) {
    this.listener = paramActionListener;

    this.userInputButton = new JButton("Input", new ImageIcon(getInputImage(this.inputOverride, this.inputState)));
    this.userInputButton.addActionListener(paramActionListener);
    this.userInputButton.setFocusable(false);

    this.runScriptButton = new JButton("Run", new ImageIcon(GlobalConfiguration.getImage("/resources/images/control_play_blue.png", GlobalConfiguration.Paths.ICON_PLAY)));
    this.runScriptButton.addActionListener(paramActionListener);
    this.runScriptButton.setFocusable(false);

    HomeButton localHomeButton = new HomeButton(ICON_HOME);

    setLayout(new BoxLayout(this, 2));
    setFloatable(false);
    add(localHomeButton);
    add(new AddButton(paramActionListener));
    add(Box.createHorizontalGlue());
    add(this.runScriptButton);
    add(this.userInputButton);

    updateSelection(false);
  }

  public void addTab() {
    int i = getComponentCount() - 4;
    add(new BotButton("RuneScape", ICON_BOT), i);
    validate();
    setSelection(i);
  }

  public void removeTab(int paramInt) {
    remove(paramInt);
    revalidate();
    repaint();
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        BotToolBar.this.setSelection(0);
      } } );
  }

  public void setTabLabel(int paramInt, String paramString) {
    ((BotButton)getComponentAtIndex(paramInt)).setText(paramString);
  }

  public int getCurrentTab() {
    if ((this.idx > -1) && (this.idx < getComponentCount() - 3)) {
      return this.idx;
    }
    return -1;
  }

  public int getScriptButton()
  {
    String str = this.runScriptButton.getText();
    if (str.equals("Run"))
      return 0;
    if (str.equals("Pause"))
      return 1;
    if (str.equals("Resume")) {
      return 2;
    }
    throw new IllegalStateException("Illegal script button state!");
  }

  public void setHome(boolean paramBoolean)
  {
    this.userInputButton.setEnabled(!paramBoolean);
    this.runScriptButton.setEnabled(!paramBoolean);
  }

  public void setInputState(int paramInt) {
    this.inputState = paramInt;
  }

  public void setOverrideInput(boolean paramBoolean) {
    this.inputOverride = paramBoolean;
  }

  public void updateInputButton() {
    this.userInputButton.setIcon(new ImageIcon(getInputImage(this.inputOverride, this.inputState)));
  }

  public void setScriptButton(int paramInt)
  {
    String str1;
    String str2;
    String str3;
    if (paramInt == 0) {
      str1 = "Run";
      str2 = "/resources/images/control_play_blue.png";
      str3 = GlobalConfiguration.Paths.ICON_PLAY;
    } else if (paramInt == 1) {
      str1 = "Pause";
      str2 = "/resources/images/control_pause.png";
      str3 = GlobalConfiguration.Paths.ICON_PAUSE;
    } else if (paramInt == 2) {
      str1 = "Resume";
      str2 = "/resources/images/control_play_blue.png";
      str3 = GlobalConfiguration.Paths.ICON_PLAY;
    } else {
      throw new IllegalArgumentException("Illegal button state: " + paramInt + "!");
    }

    this.runScriptButton.setText(str1);
    try {
      this.runScriptButton.setIcon(new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? getClass().getResource(str2) : new File(str3).toURI().toURL()));
    } catch (MalformedURLException localMalformedURLException) {
      localMalformedURLException.printStackTrace();
    }
    revalidate();
  }

  private void setSelection(int paramInt) {
    updateSelection(true);
    this.idx = paramInt;
    updateSelection(false);
    this.listener.actionPerformed(new ActionEvent(this, 0, "Tab"));
  }

  private void updateSelection(boolean paramBoolean) {
    int i = getCurrentTab();
    if (i >= 0) {
      getComponent(i).setEnabled(paramBoolean);
      getComponent(i).repaint();
    }
  }

  private Image getInputImage(boolean paramBoolean, int paramInt) {
    if ((paramBoolean) || (paramInt == 3))
      return GlobalConfiguration.getImage("/resources/images/tick.png", GlobalConfiguration.Paths.ICON_TICK);
    if (paramInt == 2)
      return GlobalConfiguration.getImage("/resources/images/keyboard.png", GlobalConfiguration.Paths.ICON_KEYBOARD);
    if (paramInt == 1) {
      return GlobalConfiguration.getImage("/resources/images/mouse.png", GlobalConfiguration.Paths.ICON_MOUSE);
    }
    return GlobalConfiguration.getImage("/resources/images/delete.png", GlobalConfiguration.Paths.ICON_DELETE);
  }

  static
  {
    ImageIcon localImageIcon = null;
    try {
      localImageIcon = new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? BotToolBar.class.getResource("/resources/images/home.png") : new File(GlobalConfiguration.Paths.ICON_HOME).toURI().toURL());
    } catch (MalformedURLException localMalformedURLException1) {
    }
    ICON_HOME = localImageIcon;
    try {
      localImageIcon = new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? BotToolBar.class.getResource("/resources/images/bot.png") : new File(GlobalConfiguration.Paths.ICON_BOT).toURI().toURL());
    } catch (MalformedURLException localMalformedURLException2) {
      localImageIcon = null;
    }
    ICON_BOT = localImageIcon;
    try {
      localImageIcon = new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? BotToolBar.class.getResource("/resources/images/close.png") : new File(GlobalConfiguration.Paths.ICON_CLOSE).toURI().toURL());
    } catch (MalformedURLException localMalformedURLException3) {
    }
    IMAGE_CLOSE = localImageIcon == null ? null : localImageIcon.getImage();
    try {
      localImageIcon = new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? BotToolBar.class.getResource("/resources/images/close_over.png") : new File(GlobalConfiguration.Paths.ICON_CLOSE_OVER).toURI().toURL());
    } catch (MalformedURLException localMalformedURLException4) {
    }
    IMAGE_CLOSE_OVER = localImageIcon == null ? null : localImageIcon.getImage();
  }

  private static class AddButton extends JComponent
  {
    private static final long serialVersionUID = 1L;
    private static final Image ICON;
    private static final Image ICON_OVER;
    private static final Image ICON_DOWN;
    private boolean hovered = false;
    private boolean pressed = false;

    public AddButton(ActionListener paramActionListener)
    {
      setPreferredSize(new Dimension(20, 20));
      setMaximumSize(new Dimension(20, 20));
      setFocusable(false);
      addMouseListener(new MouseAdapter(paramActionListener) {
        public void mouseEntered(MouseEvent paramMouseEvent) {
          BotToolBar.AddButton.access$602(BotToolBar.AddButton.this, true);
          BotToolBar.AddButton.this.repaint();
        }

        public void mouseExited(MouseEvent paramMouseEvent) {
          BotToolBar.AddButton.access$602(BotToolBar.AddButton.this, false);
          BotToolBar.AddButton.this.repaint();
        }

        public void mousePressed(MouseEvent paramMouseEvent) {
          BotToolBar.AddButton.access$702(BotToolBar.AddButton.this, true);
          BotToolBar.AddButton.this.repaint();
        }

        public void mouseReleased(MouseEvent paramMouseEvent) {
          BotToolBar.AddButton.access$702(BotToolBar.AddButton.this, false);
          BotToolBar.AddButton.this.repaint();
          this.val$listener.actionPerformed(new ActionEvent(this, paramMouseEvent.getID(), "File.New Bot"));
        } } );
    }

    public void paintComponent(Graphics paramGraphics) {
      super.paintComponent(paramGraphics);
      if (this.pressed)
        paramGraphics.drawImage(ICON_DOWN, 2, 2, null);
      else if (this.hovered)
        paramGraphics.drawImage(ICON_OVER, 2, 2, null);
      else
        paramGraphics.drawImage(ICON, 2, 2, null);
    }

    static
    {
      Image localImage = null;
      try {
        localImage = new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? BotToolBar.class.getResource("/resources/images/add.png") : new File(GlobalConfiguration.Paths.ICON_ADD).toURI().toURL()).getImage();
      } catch (MalformedURLException localMalformedURLException1) {
      }
      ICON = localImage;
      try {
        localImage = new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? BotToolBar.class.getResource("/resources/images/add_over.png") : new File(GlobalConfiguration.Paths.ICON_ADD_OVER).toURI().toURL()).getImage();
      } catch (MalformedURLException localMalformedURLException2) {
        localImage = null;
      }
      ICON_OVER = localImage;
      try {
        localImage = new ImageIcon(GlobalConfiguration.RUNNING_FROM_JAR ? BotToolBar.class.getResource("/resources/images/add_down.png") : new File(GlobalConfiguration.Paths.ICON_ADD_DOWN).toURI().toURL()).getImage();
      } catch (MalformedURLException localMalformedURLException3) {
        localImage = null;
      }
      ICON_DOWN = localImage;
    }
  }

  private class BotButton extends JPanel
  {
    private static final long serialVersionUID = 329845763420L;
    private JLabel nameLabel;
    private boolean hovered;
    private boolean close;

    public BotButton(String paramIcon, Icon arg3)
    {
      super();
      setBorder(new EmptyBorder(3, 6, 2, 3));
      this.nameLabel = new JLabel(paramIcon);
      Icon localIcon;
      this.nameLabel.setIcon(localIcon);
      this.nameLabel.setPreferredSize(new Dimension(85, 22));
      this.nameLabel.setMaximumSize(new Dimension(85, 22));
      add(this.nameLabel, "West");

      setPreferredSize(new Dimension(110, 22));
      setMaximumSize(new Dimension(110, 22));
      setFocusable(false);
      addMouseListener(new MouseAdapter(BotToolBar.this) {
        public void mouseReleased(MouseEvent paramMouseEvent) {
          if ((BotToolBar.BotButton.this.hovered) && (BotToolBar.BotButton.this.close)) {
            int i = BotToolBar.this.getComponentIndex(BotToolBar.BotButton.this);
            BotToolBar.this.listener.actionPerformed(new ActionEvent(this, 1001, "Close." + i));
          }
          else {
            BotToolBar.this.setSelection(BotToolBar.this.getComponentIndex(BotToolBar.BotButton.this));
          }
        }

        public void mouseEntered(MouseEvent paramMouseEvent) {
          BotToolBar.BotButton.access$302(BotToolBar.BotButton.this, true);
          BotToolBar.BotButton.this.repaint();
        }

        public void mouseExited(MouseEvent paramMouseEvent) {
          BotToolBar.BotButton.access$302(BotToolBar.BotButton.this, false);
          BotToolBar.BotButton.this.repaint();
        }
      });
      addMouseMotionListener(new MouseMotionAdapter(BotToolBar.this) {
        public void mouseMoved(MouseEvent paramMouseEvent) {
          BotToolBar.BotButton.access$402(BotToolBar.BotButton.this, paramMouseEvent.getX() > 95);
          BotToolBar.BotButton.this.repaint();
        } } );
    }

    public void setText(String paramString) {
      this.nameLabel.setText(paramString);
    }

    public void paintComponent(Graphics paramGraphics) {
      super.paintComponent(paramGraphics);
      ((Graphics2D)paramGraphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      if (BotToolBar.this.getComponentIndex(this) == BotToolBar.this.idx) {
        paramGraphics.setColor(new Color(255, 255, 255, 200));
        paramGraphics.fillRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
        paramGraphics.setColor(new Color(180, 180, 180, 200));
        paramGraphics.drawRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
      } else if (this.hovered) {
        paramGraphics.setColor(new Color(255, 255, 255, 150));
        paramGraphics.fillRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
        paramGraphics.setColor(new Color(180, 180, 180, 150));
        paramGraphics.drawRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
      } else {
        paramGraphics.setColor(new Color(180, 180, 180, 150));
        paramGraphics.drawRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
      }
      if ((this.hovered) && (this.close))
        paramGraphics.drawImage(BotToolBar.IMAGE_CLOSE_OVER, 90, 3, null);
      else
        paramGraphics.drawImage(BotToolBar.IMAGE_CLOSE, 90, 3, null);
    }
  }

  private class HomeButton extends JPanel
  {
    private static final long serialVersionUID = 938456324328L;
    private Image image;
    private boolean hovered;

    public HomeButton(ImageIcon arg2)
    {
      super();
      Object localObject;
      this.image = localObject.getImage();
      setBorder(new EmptyBorder(3, 6, 2, 3));
      setPreferredSize(new Dimension(24, 22));
      setMaximumSize(new Dimension(24, 22));
      setFocusable(false);
      addMouseListener(new MouseAdapter(BotToolBar.this) {
        public void mouseReleased(MouseEvent paramMouseEvent) {
          BotToolBar.this.setSelection(BotToolBar.this.getComponentIndex(BotToolBar.HomeButton.this));
        }

        public void mouseEntered(MouseEvent paramMouseEvent) {
          BotToolBar.HomeButton.access$102(BotToolBar.HomeButton.this, true);
          BotToolBar.HomeButton.this.repaint();
        }

        public void mouseExited(MouseEvent paramMouseEvent) {
          BotToolBar.HomeButton.access$102(BotToolBar.HomeButton.this, false);
          BotToolBar.HomeButton.this.repaint();
        } } );
    }

    public void paintComponent(Graphics paramGraphics) {
      super.paintComponent(paramGraphics);
      ((Graphics2D)paramGraphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      if (BotToolBar.this.getComponentIndex(this) == BotToolBar.this.idx) {
        paramGraphics.setColor(new Color(255, 255, 255, 200));
        paramGraphics.fillRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
        paramGraphics.setColor(new Color(180, 180, 180, 200));
        paramGraphics.drawRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
      } else if (this.hovered) {
        paramGraphics.setColor(new Color(255, 255, 255, 150));
        paramGraphics.fillRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
        paramGraphics.setColor(new Color(180, 180, 180, 150));
        paramGraphics.drawRoundRect(0, 0, getWidth() - 2, getHeight() - 1, 4, 4);
      }
      paramGraphics.drawImage(this.image, 3, 3, null);
    }
  }
}